﻿from google.appengine.ext import webapp
from utilities import *
from entities import *
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp                                        
import os
from google.appengine.api import users
class Form(webapp.RequestHandler):
    def get(self):
        key = Entities.GetKey()
        LinhVucs = LinhVucEntities.Get(key,0)
        QuiMos = QuiMoEntities.Get(key,0)
        template_values = {
            'LinhVuc': LinhVucs,
            'QuiMo': QuiMos,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()
            }
        path = os.path.join(os.path.dirname(__file__), '../templates/formCompany.html')
        self.response.out.write(template.render(path, template_values))

class Details(webapp.RequestHandler):
    def get(self):
        NhaTuyenDungID = int(self.request.get("company_id"))
        #dondangtuyen = DonDangTuyenEntities.Get(Entities.GetKey(),0,DonDangTuyenID,"","","",0,0,0,0,0,0,0,0)
        #NhaTuyenDungID = dondangtuyen[0]['NhaTuyenDungID']
        nhatuyendung = NhaTuyenDungEntities.Get(Entities.GetKey(),"", "", 0, 0, 0, NhaTuyenDungID)
        dondangtuyen = DonDangTuyenEntities.Get(Entities.GetKey(),NhaTuyenDungID,0,"","","",0,0,0,0,0,0,0,0)
        template_values = {
            'menu' : HtmlTemplate.Menu(),
            'DonDangTuyen': dondangtuyen,
            'menu' : HtmlTemplate.Menu(),
            'NhaTuyenDung': nhatuyendung,
            'footer': HtmlTemplate.Footer()            
            }        
        
        path = os.path.join(os.path.dirname(__file__), '../templates/detailCompany.html')
        self.response.out.write(template.render(path, template_values))  

class Sign(webapp.RequestHandler):
    def post(self):
        key = Entities.GetKey()
        TenThuongHieu =  self.request.get('txtName')
        MieuTa =  self.request.get('txtDescription')
        Email =  users.get_current_user().nickname()
        SoDienThoai =  self.request.get('txtPhone')
        LinhVucID =  self.request.get('ddlLinhVuc')   
        NguoiLienHe =  self.request.get('txtContact')
        QuiMoID =  self.request.get('ddlQuiMo')
        Website =  self.request.get('txtWebsite')
        AnhDaiDien = self.request.get("img")            
        lat = self.request.get('txtLat')
        lng = self.request.get('txtLng')
        ViTri = db.GeoPt(lat=float(lat), lon=float(lng))
        ViTriText = self.request.get('txtViTri')
        key  = Entities.GetKey()
        TrangThaiID = TrangThaiEntities.GetID(Entities.GetKey(),u'Vừa nhập');
        status = NhaTuyenDungEntities.Add(key,TenThuongHieu,MieuTa,Email, SoDienThoai,int(LinhVucID),NguoiLienHe,int(QuiMoID),AnhDaiDien,Website,ViTriText,ViTri, TrangThaiID, 0)

        template_values = {
            'status' : status,
            'menu' : HtmlTemplate.Menu(),
            'footer': HtmlTemplate.Footer()
            }
        path = os.path.join(os.path.dirname(__file__), '../templates/signCompany.html')
        self.response.out.write(template.render(path, template_values))
        
class Image (webapp.RequestHandler):
    def get(self):
        nhatuyendung = db.get(self.request.get("img_id"))
        if nhatuyendung.AnhDaiDien:
            #self.response.out.write(nhatuyendung.Email)
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(nhatuyendung.AnhDaiDien)
        else:
            self.response.out.write("No image")
       